<?php

/**
 * Zend_View_Interface
 */
require_once 'Zend/View/Interface.php';

/**
 * Smarty
 */
require_once 'Smarty/Smarty.class.php';

class Smarty_Config implements Zend_View_Interface {
	/**
	 * Smarty object
	 * @var Smarty
	 */
	public $_smarty;
	
	private $css_list = array ();
	private $script_list = array ();
	
	private $render_path = null;
	
	/**
	 * Constructor.
	 *
	 * @param array $extraParams
	 * @return void
	 */
	public function __construct($extraParams = array()) {
		$this->_smarty = new Smarty ();
		foreach ( $extraParams as $key => $value ) {
			$this->_smarty->$key = $value;
		}
	}
	
	/**
	 * Return the template engine object.
	 *
	 * @return Smarty
	 */
	public function getEngine() {
		return $this->_smarty;
	}
	
	/**
	 * Sets the base directory path to templates.
	 *
	 * @param   string  $path
	 * @return  Custom_View_Smarty
	 */
	public function setBasePath($path, $classPrefix = 'Zend_View') {
		$path;
		$classPrefix;
	}
	
	/**
	 * Alias of setBasePath() method.
	 *
	 * @param   string  $path
	 * @return  Custom_View_Smarty
	 */
	public function addBasePath($path, $classPrefix = 'Zend_View') {
		$classPrefix;
		$this->setBasePath ( $path );
		return $this;
	}
	
	/**
	 * Sets the directory path to templates.
	 *
	 * @param   string  $path
	 * @return  Custom_View_Smarty
	 */
	public function setTemplateDir($path) {
		if (is_dir ( $path ) && is_readable ( $path )) {
			$this->_smarty->template_dir = $path;
			$this->_smarty->config_dir = $path;
			return $this;
		}
		throw new Exception ( 'Invalid path provided' );
	}
	
	/**
	 * Sets the directory path to compiled templates.
	 *
	 * @param   string  $path
	 * @return  Custom_View_Smarty
	 */
	public function setCompileDir($path) {
		if (is_dir ( $path ) && is_writable ( $path )) {
			$this->_smarty->compile_dir = $path;
			return $this;
		}
		throw new Exception ( 'Invalid path provided' );
	}
	
	/**
	 * Sets the directory path to cache templates.
	 *
	 * @param   string  $path
	 * @return  Custom_View_Smarty
	 */
	public function setCacheDir($path) {
		if (is_dir ( $path ) && is_writable ( $path )) {
			$this->_smarty->cache_dir = $path;
			return $this;
		}
		throw new Exception ( 'Invalid path provided' );
	}
	
	/**
	 * Alias of setTemplateDir() method.
	 *
	 * @param   string  $path
	 * @return  Custom_View_Smarty
	 */
	public function setScriptPath($path) {
		$this->setTemplateDir ( $path );
		return $this;
	}
	
	/**
	 * Returns an array of the directory path to templates.
	 *
	 * @return  array
	 */
	public function getScriptPaths() {
		return array ($this->_smarty->template_dir );
	}
	
	/**
	 * Assign a variable to the template
	 *
	 * @param string $key The variable name.
	 * @param mixed $val The variable value.
	 * @return void
	 */
	public function __set($key, $val) {
		$this->_smarty->assign ( $key, $val );
	}
	
	/**
	 * Retrieve an assigned variable
	 *
	 * @param string $key The variable name.
	 * @return mixed The variable value.
	 */
	public function __get($key) {
		return $this->_smarty->get_template_vars ( $key );
	}
	
	/**
	 * Allows testing with empty() and isset() to work
	 *
	 * @param string $key
	 * @return boolean
	 */
	public function __isset($key) {
		return (null !== $this->_smarty->get_template_vars ( $key ));
	}
	
	/**
	 * Allows unset() on object properties to work
	 *
	 * @param string $key
	 * @return void
	 */
	public function __unset($key) {
		$this->_smarty->clear_assign ( $key );
	}
	
	/**
	 * Assign variables to the template
	 *
	 * Allows setting a specific key to the specified value, OR passing an array
	 * of key => value pairs to set en masse.
	 *
	 * @see __set()
	 * @param string|array $spec The assignment strategy to use (key or array of key
	 * => value pairs)
	 * @param mixed $value (Optional) If assigning a named variable, use this
	 * as the value.
	 * @return void
	 * 
	 */
	public function assign($spec, $value = null) {
		if (is_array ( $spec )) {
			$this->_smarty->assign ( $spec );
			return;
		}
		
		$this->_smarty->assign ( $spec, $value );
	}
	
	/**
	 * Clear all assigned variables
	 *
	 * Clears all variables assigned to Zend_View either via {@link assign()} or
	 * property overloading ({@link __get()}/{@link __set()}).
	 * 
	 * @return void
	 */
	public function clearVars() {
		$this->_smarty->clear_all_assign ();
	}
	
	/**
	 * Processes a template and returns the output.
	 * 
	 * @param string $name The template to process.
	 * @return string The output.
	 */
	public function render($name) {
		if ($this->render_path) {
			$_tpl_name_arr = explode ( '/', $name );
			$name = $this->render_path . $_tpl_name_arr [count ( $_tpl_name_arr ) - 1];
		}
		return $this->_smarty->fetch ( $name );
	}
	
	public function fetch($name) {
		return $this->_smarty->fetch ( $name );
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $resource_name
	 * @param unknown_type $cache_id
	 * @param unknown_type $compile_id
	 * @return unknown
	 */
	public function display($resource_name, $cache_id = null, $compile_id = null) {
		return $this->_smarty->display ( $resource_name, $cache_id, $compile_id );
	}
	
	/**
	 * 页面跳转
	 *
	 * @param String $rdtrul 将要跳转的地址
	 * @param unknown_type $msg 跳转页面提示信息
	 * @param unknown_type $baseurl 跳转地址是否自动添加 根域地址
	 * @param unknown_type $iheader 跳转页面是否为"富"页面
	 * @param unknown_type $wsec 跳转等候时间
	 */
	public function perRedirect($msg, $url_forward = 'goback', $ms = 1250, $dialog = '', $returnjs = '') {
		$this->_smarty->assign ( 'msg', $msg );
		$this->_smarty->assign ( 'url_forward', $url_forward );
		$this->_smarty->assign ( 'ms', $ms );
		$this->_smarty->assign ( 'dialog', $dialog );
		$this->_smarty->assign ( 'returnjs', $returnjs );
		
		$this->_smarty->display ( 'message.html' );
		exit ();
	}
	
	public function getTpl($tpl_type, $tpl_id) {
	
	}
	
	/**
	 * 添加一个CSS样式文件到文件头部中
	 *
	 * @param unknown_type $cssname
	 * @param unknown_type $ver
	 */
	public function setCssFile($cssname, $ver = 0) {
		$this->css_list [$cssname] = $ver;
		$this->_smarty->assign ( 'cssList', $this->css_list );
		return true;
	}
	/**
	 * 移除一个CSS样式文件
	 *
	 * @param unknown_type $cssname
	 */
	public function removeCssFile($cssname) {
		if (isset ( $this->css_list [$cssname] )) {
			unset ( $this->css_list [$cssname] );
			$this->_smarty->assign ( 'cssList', $this->css_list );
		}
		return true;
	}
	
	/**
	 * 设置script文件
	 *
	 * @param unknown_type $scriptname
	 * @param unknown_type $ver
	 * @return unknown
	 */
	public function setScriptFile($scriptname, $ver = 0) {
		$this->script_list [$scriptname] = $ver;
		$this->_smarty->assign ( 'scriptList', $this->script_list );
		return true;
	}
	
	public function setAutoRenderPath($path) {
		$this->render_path = $path;
	}
}